﻿window.SearchKey = null;
$.MGJ.OrganizationalManage = [];
(function () {
    var _this = $.MGJ.OrganizationalManage;
    $.extend($.MGJ.OrganizationalManage, {
        ExeclExpors: function (ele) {
          
                $("input.AllClickChild:checked").each(function () {
                    info.push($(this).attr('ID'));
                });
                if (info.length == 0) {
                    $.L.msgError("请选择要导出的数据信息！");
                    return
                }

              
            if (!$(ele).find('.no-result').hasClass('no-result')) {
            
                window.open(ACTION_URL("ExeclExpors", "Manager") + "?Osearch=" + window.SearchKey + "&Eform=" + window.Eform + "&Key=" + info);
            }
            else {
                $.L.msgWarning('导出数据不能为空！')
            }
        },
        ExeclExporSend: function (EID, sendtype) {
            window.open(ACTION_URL("ExeclExporSend", "Manager") + "?EID=" + EID + "&CollectSendType=" + sendtype);
        }
    });
})(jQuery);

(function () {
    var _this = $.MGJ.OrganizationalManage;
    $.extend($.MGJ.OrganizationalManage, {
        // 显示详细数据
        Dialog_OrganizationalShowDataObj: null,
        Dialog_OrganizationalShowData: function (obj) {
            var id = $(obj).attr("OEID");
            this.Dialog_OrganizationalShowDataObj = $.L.dialog({
                url: ACTION_URL("OrganizationalShowData", "Manager") + "?id=" + id,
                title: "详细数据",
                width: 1100,
                height: 'auto',
                buttons: {
                    "button_cancel": {
                        type: 'primary',
                        text: '返回',
                        click: 'MODALMISS'
                    }
                },
            });
        },

        // 新增机构
        Dialog_AddOrganizationalObj: null,
        Dialog_AddOrganizational: function () {

            //$my.GetPCR(0, 0);
            this.Dialog_AddOrganizationalObj = $.L.dialog({
                url: ACTION_URL("AddOrganization", "Manager"),
                title: "新增机构",
                width: 800,
                height: "auto",
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            $(".btn-primary").attr("disabled", true);
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },

        Dialog_EditOrganizationalObj: null,
        Dialog_EditOrganizational: function (obj, eid) {
         
            var id = $(obj).attr("OEID");
            this.Dialog_EditOrganizationalObj = $.L.dialog({
                url: ACTION_URL("EditOrganization", "Manager") + "?id=" + id,
                title: "编辑机构",
                width: 800,
                height: "auto",
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            var bootstrapValidator = $("#form_Enterprise").data('bootstrapValidator').validate();
                            if (bootstrapValidator.isValid()) {
                                $.MGJ.OrganizationalManage.Ajax_EditEnterprise(eid);
                                $(".btn-primary").attr("disabled", true);
                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                            };
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        }
        ,
        Dialog_SendReceiveAddressObj: null,
        Dialog_SendReceiveAddress: function (obj) {
            var id = $(obj).attr("OEID");
            this.Dialog_SendReceiveAddressObj = $.L.dialog({
                url: ACTION_URL("SendReceiveAddress", "Manager") + "?id=" + id,
                title: "收发货地址",
                width: 1000,
                height: "auto",
                buttons: {
                    //"button_save": {
                    //    type: 'primary',
                    //    text: '保存',
                    //    click: function () {

                    //    }
                    //},
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        }
           ,
        Dialog_AddSendAddressObj: null,
        Dialog_AddSendAddress: function (EID, obj, sendtype) {
            var id = $(obj).attr("ID");
            this.Dialog_AddSendAddressObj = $.L.dialog({
                url: ACTION_URL("AddSendAddress", "Manager") + "?ID=" + id,
                title: "新增发货地址",
                width: 800,
                height: "auto",
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            $("#EnterpriseIdOut").val(EID);
                            var bootstrapValidator = $("#form_AddOutAddress").data('bootstrapValidator').validate();
                            if (bootstrapValidator.isValid()) {
                                _this.Ajax_AddSendInfo(EID, 1);
                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                            };
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        }
            ,
        Dialog_AddReAddressObj: null,
        Dialog_AddReAddress: function (EID, obj, sendtype) {
            var id = $(obj).attr("ID");
            this.Dialog_AddReAddressObj = $.L.dialog({
                url: ACTION_URL("AddReAddress", "Manager") + "?ID=" + id,
                title: "新增收货地址",
                width: 800,
                height: "auto",
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '保存',
                        click: function () {
                            $("#EnterpriseId").val(EID);
                            var bootstrapValidator = $("#AddAddress").data('bootstrapValidator').validate();
                            if (bootstrapValidator.isValid()) {
                                _this.Ajax_AddSendInfo(EID, 0);
                            }
                            else {
                                $.L.msgError("请检查数据格式！");
                            };
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });

        }
    });
})(jQuery);

(function () {
    var _index = 1;
    var _this = $.MGJ.OrganizationalManage;
    $.extend($.MGJ.OrganizationalManage, {
        //分页查询
        //查询机构
        Ajax_GetOrganizationalInfo: function (pageIndex) {
            _index = pageIndex;
            window.SearchKey = removeSpace($("#Osearch").val());
            window.Eform = $("#OrganizationForm").val();
            $.L.ajaxHandle({
                url: ACTION_URL("GetOrganizationalInfo", "Manager"),
                data: {
                    Eform: window.Eform,
                    Osearch: window.SearchKey,
                    pageIndex: pageIndex,
                    pageSize: 10
                },
                success: function (response) {
                    $("#OMID").L_bindingDataList(response);
                    $("#OMID_left").L_bindingDataList(response);
                    $("#OMID_right").L_bindingDataList(response);
                }
            });
        },
        Ajax_DelOrganizational: function (obj) {
            $.L.msgConfirm("确认删除该记录?", function () {
                var id = $(obj).attr("OEID");
                $.L.ajaxHandle({
                    url: ACTION_URL("DelOrganizational", "Manager"),
                    data: {
                        oeid: id
                    },
                    success: function (response) {
                        if (response > 0) {
                            _this.Ajax_GetOrganizationalInfo(_index);
                            $.L.msgSuccess("操作成功");
                        }
                        else {
                            $.L.msgError("操作失败");
                        }
                    }
                });
            })

        },
        Ajax_AddEnterprise: function (data) {
            //var myDate = new Date();
            //var lateTime = $("#form_Enterprise .row").find('input[name="endDate"]:checked').val();
            //$("#form_Enterprise .row").find('input[type="hidden"][name="StartTime"]').val(myDate.toLocaleDateString());
            //$("#form_Enterprise .row").find('input[type="hidden"][name="EndTime"]').val(new Date(myDate.getFullYear() + parseInt(lateTime), myDate.getMonth(), myDate.getDate()).toLocaleDateString());

            //var data=JSON.stringify($("#form_Enterprise").serializeObject()

            $.L.ajaxHandle({
                url: ACTION_URL("AddEnterprise", "Manager"), //+ "?loginName=" + $("#loginName").val() + "&pwd=" + $("#pwd").val(),
                data: { submitData: data },
                type: "POST",
                success: function (response) {
                    _this.Dialog_AddOrganizationalObj.close();
                    if (response.state > 0) {
                        _this.Ajax_GetOrganizationalInfo(1);
                        $.L.msgSuccess("公司名称：" + response.ename + ",该公司的管理账号为：" + response.LoginName + "密码为：" + response.pwd);
                    }
                    else {
                        $.L.msgError("操作失败");
                        $('button[data-btnname="button_save"]').attr("disabled", false);
                    }
                }
            });
        },

        Ajax_EditEnterprise: function (eId) {

            if (eId == 1) {
                //编辑更新日期
                var eyear = $("#form_Enterprise .row").find('input[type="hidden"][name="EndTime"]').val().split(" ")[0].split('/')[0];
                var lateTime = $("#form_Enterprise .row").find('input[name="endDate"]:checked').val();
                var syear = $("#form_Enterprise .row").find('input[type="hidden"][name="StartTime"]').val().split(" ")[0].split('/')[0];
                var smonth = $("#form_Enterprise .row").find('input[type="hidden"][name="StartTime"]').val().split(" ")[0].split('/')[1];
                var sday = $("#form_Enterprise .row").find('input[type="hidden"][name="StartTime"]').val().split(" ")[0].split('/')[2];
                if (parseInt(eyear) - parseInt(syear) != parseInt(lateTime)) {
                    $("#form_Enterprise .row").find('input[type="hidden"][name="EndTime"]').val(new Date(parseInt(syear) + parseInt(lateTime), parseInt(smonth), parseInt(sday)).toLocaleDateString());
                }
            }

            var clfId = $('#ClassifyId').val();
            var str = "";
            for (var i = 0; i < clfId.length; i++) {
                str += clfId[i] + ","
            }
            str = (str.substring(str.length - 1) == ',') ? str.substring(0, str.length - 1) : str;

            var ServiceVersion = $('#ServiceVersion').val();
            var str2 = "";
            for (var i = 0; i < ServiceVersion.length; i++) {
                str2 += ServiceVersion[i] + ","
            }
            str2 = (str2.substring(str2.length - 1) == ',') ? str2.substring(0, str2.length - 1) : str2;




            var data = $("#form_Enterprise").serializeObject();
            data.ClassifyId = str;
            data.ServiceVersion = str2;

            $.ajax({
                type: "POST",
                dataType: "json",
                url: "/Manager/UpdateEnterprise",
                data: data,
                success: function (response) {
                    _this.Dialog_EditOrganizationalObj.close();

                    if (response > 0) {
                        _this.Ajax_GetOrganizationalInfo(1);
                        $.L.msgSuccess("操作成功！");
                    }
                    else {
                        $.L.msgError("操作失败");
                    }
                }


            });
            //var data = $("#form_Enterprise").serialize()
            //$.L.ajaxHandle({
            //    url: ACTION_URL("updEnterprise", "Manager"), //+ "?loginName=" + $("#loginName").val() + "&pwd=" + $("#pwd").val(),
            //    data:data,
            //    type: "POST",
            //    success: function (response) {
            //        _this.Dialog_EditOrganizationalObj.close();

            //        if (response > 0) {
            //            _this.Ajax_GetOrganizationalInfo(1);
            //            $.L.msgSuccess("操作成功！");
            //        }
            //        else {
            //            $.L.msgError("操作失败");
            //        }
            //    }
            //});
        },
        Ajax_GetSendInfo: function (EID, sendtype) {
            $.L.ajaxHandle({
                url: ACTION_URL("GetSendInfo", "Manager"),
                data: {
                    EID: EID,
                    CollectSendType: sendtype
                },
                success: function (response) {
                    if (sendtype == 0) $("#SendInfo").L_bindingDataList(response);
                    else $("#SendOutInfo").L_bindingDataList(response);
                }
            });
        },
        Ajax_AddSendInfo: function (EID, sendtype) {
            if (sendtype == 0) {
                $.L.ajaxHandle({
                    url: ACTION_URL("AddSendInfo", "Manager"),
                    data: $("#AddAddress").serialize(),
                    success: function (response) {
                        _this.Dialog_AddReAddressObj.close();
                        if (response > 0) {
                            _this.Ajax_GetSendInfo(EID, sendtype);
                            $.L.msgSuccess("操作成功");
                        }
                        else {
                            $.L.msgSuccess("操作失败");
                        }
                    }
                });
            } else {
                $.L.ajaxHandle({
                    url: ACTION_URL("AddSendInfo", "Manager"),
                    data: $("#form_AddOutAddress").serialize(),
                    success: function (response) {
                        _this.Dialog_AddSendAddressObj.close();
                        if (response > 0) {
                            _this.Ajax_GetSendInfo(EID, sendtype);
                            $.L.msgSuccess("操作成功");
                        }
                        else {
                            $.L.msgSuccess("操作失败");
                        }
                    }
                });
            }

        },
        Ajax_DelSendInfo: function (EID, obj, sendtype) {
            $.L.msgConfirm("确认删除该记录?", function () {
                var id = $(obj).attr("ID");
                $.L.ajaxHandle({
                    url: ACTION_URL("DelSendInfo", "Manager"),
                    data: {
                        ID: id
                    },
                    success: function (response) {
                        if (response > 0) {
                            _this.Ajax_GetSendInfo(EID, sendtype);
                            $.L.msgSuccess("操作成功");
                        }
                        else {
                            $.L.msgError("操作失败");
                        }
                    }
                });
            })

        },
    });
})(jQuery);

//表单转JSON字符串  var data = JSON.stringify($("form").serializeObject()) ;
$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};


(function () {
    var _this = $.MGJ.OrganizationalManage;
    $.extend($.MGJ.OrganizationalManage, {
        Bin_ViewFlag: function (text) {

            return text == 1 ? "启用" : "禁用";
        },
        //获取下拉框
        Ajax_GetFormBox: function (rid,id, text, HeadId) {
       

            var txt = text || "请选择";
            $.L.ajaxHandle({
                url: ACTION_URL("GetFormBox", "Manager"),
                data:{rid:rid},
                success: function (response) {
                    var $box = id == 0 ? "<option value=''   selected >--" + txt + "--</option>" : "";
                    $(response).each(function (i, e) {
                        
                        if (e.ID == id) {

                            $box += " <option value='" + e.ID + "' selected > " + e.Name + "</option>";
                        }
                        //else if (e.ID == 1 && HeadId!=0) { //子机构不需要政府机构形式
                           
                        //}
                        else
                            $box += " <option value='" + e.ID + "' > " + e.Name + "</option>";
                    })
                    $("#OrganizationForm").html($box);
                }
            });
        },
        Ajax_GetTypeBox: function (FormId, id) {
   
            var $box = "";
            FormId = $.isNumeric(FormId) ?FormId   : 0;
             
            if (FormId != 0) {

                $.L.ajaxHandle({
                    url: ACTION_URL("GetTypeBox", "Manager"),
                    data: {
                        ID: FormId
                    },
                    success: function (response) {

                        $(response).each(function (i, e) {
                            if (e.ID == id) {
                                $box += " <option value='" + e.ID + "' selected > " + e.Name + "</option>";
                            }
                            else
                                $box += " <option value='" + e.ID + "' > " + e.Name + "</option>";
                        })
                        $("#OrganizationId").html($box);
                    }
                });
            }
            else {
                $box += " <option value=''   selected >--请选择机构形式--</option>";
                $("#OrganizationId").html($box);
            }

       

        },
        Ajax_UpLoad: function (obj) {

            $.L.ajaxHandle({
                url: ACTION_URL("UploadImg", "DBH"),
                data: {
                    fname: obj
                },
                success: function () {
                    $("#OMID").L_bindingDataList(response);
                    $("#OMID_left").L_bindingDataList(response);
                    $("#OMID_right").L_bindingDataList(response);
                }
            });
        },
        Ajax_GetRoleTree: function () {

        }
    });
})(jQuery);

(function () {
    var _this = $.MGJ.OrganizationalManage;
   
})(jQuery);
